package com.xyzk.diagnose.tools;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.xyzk.diagnose.entity.CheryMesInfo;
import com.xyzk.diagnose.entity.EcuInfo;

import android.util.Xml;

public class CommonXml {

	public static String getEmsInfoxml(String xml){
		String ems = null;
		if (xml !=null){
			//设置输入的内容
			try {
				int v_start = xml.indexOf("<Ecoscode3>");
				int v_end = xml.indexOf("</Ecoscode3>");
				if(v_start > 0 && v_end > v_start)
					ems = xml.substring(v_start + 11,v_end);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ems;
	}

	public static CheryMesInfo GetEcuInfoxml(String Pxmlstr)
	{
		CheryMesInfo mesinfo = new CheryMesInfo();
		List<EcuInfo> ecuinfolist = new ArrayList<EcuInfo>();
		//获取XML文件
		//String xmlstr = HttpRequest.sendRequestWithHttpURLConnection(new String(Common.cartype.vin),Common.Mes_version + "/Get_Electric");
		//XmlPullParserFactory factory = null;
		XmlPullParser xmlPullParser = null;
		if(Pxmlstr != null) //ok
		{
			try {
				//factory = XmlPullParserFactory.newInstance();
				//xmlPullParser = factory.newPullParser();
				xmlPullParser = Xml.newPullParser();
				//设置输入的内容
				xmlPullParser.setInput(new StringReader(Pxmlstr));
				// 得到当前事件类型
				int eventType = xmlPullParser.getEventType();
				EcuInfo ecuinfo = null;
				String name = null;
				while (eventType != XmlPullParser.END_DOCUMENT)
				{
					switch (eventType) {
						case XmlPullParser.START_DOCUMENT:
							break;
						case XmlPullParser.START_TAG:
							//获取名称
							name = xmlPullParser.getName();
							if ("ECU".equals(name))
							{
								ecuinfo = new EcuInfo();
								String v_t1 = xmlPullParser.getAttributeValue(null, "name");
								//String v_t2 = xmlPullParser.getAttributeName(0);
								ecuinfo.setname(v_t1);
							}
							else if("Parameter".equals(name))
							{
								if("Partnumber".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.setsn(vx);
								}
								else if("HARDWARE1".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.sethardver1(vx);
								}
								else if("HARDWARE2".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.sethardver2(vx);
								} else if ("HARDWARE3".equals(xmlPullParser.getAttributeValue(null, "name"))) {
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.sethardver3(vx);
								} else if("SOFTWARE1".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver1(vx);
								}
								else if("SOFTWARE2".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver2(vx);
								}else if("SOFTWARE3".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.setsoftver3(vx);
								}
								else if("Code".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.setflashcode(vx);
								}
								else if("DateName".equals(xmlPullParser.getAttributeValue(null, "name")))
								{
									String vx = xmlPullParser.nextText();
									if(vx != null)
									{
										byte [] vbyte = vx.getBytes();
										Cheanfocus(vbyte);;
										vx = new String(vbyte);
									}
									ecuinfo.setflashname(vx);
								}
							}
							else if(name.equals("VinCode"))
							{
								String v_text = xmlPullParser.nextText();
								mesinfo.setvin(v_text);
							}
							else if(name.equals("MaterialCode"))
							{
								mesinfo.setMaterialCode(xmlPullParser.nextText());
							}
							else if(name.equals("CarType"))
							{
								mesinfo.setCarType(xmlPullParser.nextText());
							}
							else if(name.equals("Soft_Code"))
							{
								mesinfo.setSoftCode(xmlPullParser.nextText());
							}
							else if(name.equals("Status"))
							{
								mesinfo.setStatus(xmlPullParser.nextText());
							}
							break;
						case XmlPullParser.END_TAG:
							name = xmlPullParser.getName();
							if("ECU".equals(name))
							{
								if(ecuinfo != null) //保存
								{
									ecuinfolist.add(ecuinfo);
									ecuinfo = null;
								}
							}
							break;
						case XmlPullParser.TEXT:
							break;
					}
					eventType = xmlPullParser.next();
				}


			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		mesinfo.setEcuInfo(ecuinfolist);
		return mesinfo;
	}
	@SuppressWarnings("unused")
	public static String GetEcuInfoSn(CheryMesInfo Pmesinfog,String Pname)
	{
		String v_sn = "";
		if(Pmesinfog == null) return v_sn;
		Pmesinfog.ClearEcuId();
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		for(int i = 0; i < eculist.size(); i ++)
		{
			if(eculist.get(i).getname().equals(Pname))
			{
				if(v_sn.length() < 1)
					v_sn = eculist.get(i).getsn();
				else
					v_sn += "," + eculist.get(i).getsn();
				Pmesinfog.setEcuId(i); //缓存
			}
		}
		if(v_sn == null) //没找到
			Pmesinfog.ClearEcuId();
		if(v_sn == null)
			v_sn = "";
		return v_sn;
	}
	public static String GetEcuInfoHard(CheryMesInfo Pmesinfog,String Pname)
	{
		String hard = "";
		if(Pmesinfog == null) return hard;
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		if((Pname != null) && (Pmesinfog.getEcuId() >= 0)) //直接取上次搜索结果
		{
			String hh = eculist.get(Pmesinfog.getEcuId()).gethardver1();
			if(hh != null && hh.length() > 0)
				hard = hh;
			hh = eculist.get(Pmesinfog.getEcuId()).gethardver2();
			if(hh != null && hh.length() > 0)
			{
				if(hard.length() > 0)
					hard += "," + hh;
				else
					hard = hh;
			}
			hh = eculist.get(Pmesinfog.getEcuId()).gethardver3();
			if(hh != null && hh.length() > 0)
			{
				if(hard.length() > 0)
					hard += "," + hh;
				else
					hard = hh;
			}


		}
		if((Pname != null) && (Pmesinfog.getEcuId2() >= 0))
		{
			String hh = eculist.get(Pmesinfog.getEcuId2()).gethardver1();
			if(hh != null && hh.length() > 0)
			{
				if(hard.length() > 0)
					hard += "," + hh;
				else
					hard = hh;
			}
			hh = eculist.get(Pmesinfog.getEcuId2()).gethardver2();
			if(hh != null && hh.length() > 0)
			{
				if(hard.length() > 0)
					hard += "," + hh;
				else
					hard = hh;
			}
			hh = eculist.get(Pmesinfog.getEcuId2()).gethardver3();
			if(hh != null && hh.length() > 0)
			{
				if(hard.length() > 0)
					hard += "," + hh;
				else
					hard = hh;
			}
		}
		/////
		if(Pmesinfog.getEcuId() < 0) //前面没缓存
		{
			for(int i = 0; i < eculist.size(); i ++)
			{
				if(eculist.get(i).getname().equals(Pname))
				{
					String hh = eculist.get(i).gethardver1();
					if(hh != null && hh.length() > 0)
					{
						if(hard.length() > 0)
							hard += "," + hh;
						else
							hard = hh;
					}
					hh = eculist.get(i).gethardver2();
					if(hh != null && hh.length() > 0)
					{
						if(hard.length() > 0)
							hard += "," + hh;
						else
							hard = hh;
					}
					hh = eculist.get(i).gethardver3();
					if(hh != null && hh.length() > 0)
					{
						if(hard.length() > 0)
							hard += "," + hh;
						else
							hard = hh;
					}
					Pmesinfog.setEcuId(i); //缓存
				}
			}
		}
		if(hard == null)
			hard = "";
		return hard;
	}
	public static String GetEcuInfoSoft(CheryMesInfo Pmesinfog,String Pname)
	{
		String soft = "";
		if(Pmesinfog == null) return soft;
		Pmesinfog.ClearEcuId();
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		if((Pname != null) && (Pmesinfog.getEcuId() >= 0)) //直接取上次搜索结果
		{
			String hh = eculist.get(Pmesinfog.getEcuId()).getsoftver1();
			if(hh != null && hh.length() > 0)
				soft = hh;
			hh = eculist.get(Pmesinfog.getEcuId()).getsoftver2();
			if(hh != null && hh.length() > 0)
			{
				if(soft.length() > 0)
					soft += "," + hh;
				else
					soft = hh;
			}
			hh = eculist.get(Pmesinfog.getEcuId()).getsoftver3();
			if(hh != null && hh.length() > 0)
			{
				if(soft.length() > 0)
					soft += "," + hh;
				else
					soft = hh;
			}

		}
		if((Pname != null) && (Pmesinfog.getEcuId2() >= 0))
		{
			String hh = eculist.get(Pmesinfog.getEcuId2()).getsoftver1();
			if(hh != null && hh.length() > 0)
			{
				if(soft.length() > 0)
					soft += "," + hh;
				else
					soft = hh;
			}
			hh = eculist.get(Pmesinfog.getEcuId2()).getsoftver2();
			if(hh != null && hh.length() > 0)
			{
				if(soft.length() > 0)
					soft += "," + hh;
				else
					soft = hh;
			}
			hh = eculist.get(Pmesinfog.getEcuId2()).getsoftver3();
			if(hh != null && hh.length() > 0)
			{
				if(soft.length() > 0)
					soft += "," + hh;
				else
					soft = hh;
			}
		}
		/////
		if(Pmesinfog.getEcuId() < 0) //前面没缓存
		{
			for(int i = 0; i < eculist.size(); i ++)
			{
				if(eculist.get(i).getname().equals(Pname))
				{
					String hh = eculist.get(i).getsoftver1();
					if(hh != null && hh.length() > 0)
					{
						if(soft.length() > 0)
							soft += "," + hh;
						else
							soft = hh;
					}
					hh = eculist.get(i).getsoftver2();
					if(hh != null && hh.length() > 0)
					{
						if(soft.length() > 0)
							soft += "," + hh;
						else
							soft = hh;
					}
					hh = eculist.get(i).getsoftver3();
					if(hh != null && hh.length() > 0)
					{
						if(soft.length() > 0)
							soft += "," + hh;
						else
							soft = hh;
					}
					Pmesinfog.setEcuId(i); //缓存
				}
			}
		}
		if(soft == null)
			soft = "";
		return soft;
	}
	public static String GetEcuFlashCode(CheryMesInfo Pmesinfog,String Pname)
	{
		String v_code = "";
		if(Pmesinfog == null) return v_code;
		Pmesinfog.ClearEcuId();
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		for(int i = 0; i < eculist.size(); i ++)
		{
			if(eculist.get(i).getname().equals(Pname))
			{
				v_code = eculist.get(i).getflashcode();
			}
		}
		if(v_code == null)
			v_code = "";
		return v_code;
	}
	public static String GetEcuFlashName(CheryMesInfo Pmesinfog,String Pname)
	{
		String v_code = "";
		if(Pmesinfog == null) return v_code;
		Pmesinfog.ClearEcuId();
		List<EcuInfo> eculist = Pmesinfog.getEcuInfo();
		for(int i = 0; i < eculist.size(); i ++)
		{
			if(eculist.get(i).getname().equals(Pname))
			{
				v_code = eculist.get(i).getflashname();
			}
		}
		if(v_code == null)
			v_code = "";
		return v_code;
	}
	public static void Cheanfocus(byte [] Pdata)
	{
		for(int i = 0; i < Pdata.length; i ++)
		{
			if(Pdata[i] == ' ')
				Pdata[i] = 0x00;
		}
	}
	//判断Pone是否跟Pc1 or Pc2 相同
	public static boolean StingCompare(String Pone,String Pc1,String Pc2)
	{
		boolean isok = false;
		//String v_one;
		int len1 = Pone.length();
		int len2 = 0;
		if(Pc1 != null)
			len2 = Pc1.length();
		int len3 = 0;
		if(Pc2 != null)
			len3 = Pc2.length();
		//int len = len1;
		if(len1 <= 0)
			isok = true;
		else if(len2 > 0)
		{
			//if(len > len2) 
			//	len = len2;
			//v_one = Pone.substring(0, len);
			if(Pone.equals(Pc1))
				isok = true;
			else if(len3 > 0)
			{
				//if(len > len3) 
				//	len = len3;
				//v_one = Pone.substring(0, len);
				if(Pone.equals(Pc2))
					isok = true;
			}
		}
		else if(len3 > 0)
		{
			//if(len > len3) 
			//	len = len3;
			//v_one = Pone.substring(0, len);
			if(Pone.equals(Pc2))
				isok = true;
		}
		return isok;
	}
	public static byte [] ReadFrmXmlFile(String FileName)
	{
		byte [] readbyte = null;
		String v_read = "";
		int readlen = 0;
		try {

			FileInputStream fileInputStream = new FileInputStream(FileName);
			long filelength = fileInputStream.available();
			if(filelength <= 0) return readbyte;
			byte [] readbuf = new byte[(int) (filelength)];
			readlen = fileInputStream.read(readbuf);
			String vstr = new String(readbuf);
			int v_start = vstr.indexOf("<DATEN>");
			int v_end = vstr.indexOf("</DATEN>");
			if(v_start > 0 && v_end > v_start)
				v_read = vstr.substring(v_start + 7,v_end);
			//转换
			readbyte = new byte[v_read.length() / 2];
			Commonfunc.StringToBytes(v_read, readbyte, readbyte.length);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



		return readbyte;
	}
}
